var IocContext = new Map()

class InvokeQueue{
	constructor() {
	    this._queue=[]
		this.block = false
		setInterval(()=>{
			if(this.block == false){
				let ob = this._queue.shift()
				if(ob){
					this.block = true
					setTimeout(()=>{
						this.block = false
						ob.fn()
					}, ob.time)
				}
			}
		}, 300)
	}
	delay(time, fn){
		this._queue.push({time:time, fn:fn})
	}
}

InvokeQueue.instance = new InvokeQueue()

class InnerEventBus {
	constructor(){
		this.eventListBus ={}
	}
	
	$on(event, fn){
		 let list = this.eventListBus[event] || []
		 list.push(fn)
		 this.eventListBus[event] = list
	}
	
	$emit(event, ...args){
		console.log("EventBus Emit", event, ...args)
		this.eventListBus[event] && this.eventListBus[event].forEach((fn)=>{
			fn(...args)
		})
	}
}

InnerEventBus.instance = new InnerEventBus()


class IocComponent extends React.Component {
	
	constructor(props) {
		super(props)
		if(props.id){
			IocContext.set(props.id, this)
		}
	}
	
	componentWillUnmount(){
		IocContext.delete(this.props.id)
		if( typeof this.willUnmount == 'function'){
			this.willUnmount()
		}
	}
	
	$emit(event, ...args){
		InnerEventBus.instance.$emit(event, ...args)
	}
	
	$on(event, fn){
		InnerEventBus.instance.$on(event, fn)
	}
	
	$invoke(id, fname, ...args){
		let bean = IOC.getBean(id)
		if(typeof bean[fname] === 'function'){
			bean[fname](...args)
		}
	}
}

var IOC = {}

IOC.getBean  = function(id){
	return IocContext.get(id)
}

IOC.getBeanState = function(id){
	var bean = IOC.getBean(id)
	if(bean){
		return  bean.state
	}
}

IOC.Component = IocComponent


function getDocumentPort (doc) {
    if(doc.compatMode == "BackCompat") {
		console.log(doc.body.scrollHeight)
        return {
            width: doc.body.scrollWidth,
            height: doc.body.scrollHeight
        };
    } else {
		console.log("size",doc.documentElement.scrollHeight, doc.documentElement.clientHeight, doc.documentElement.offsetHeight)
        return {
            width: Math.max(doc.documentElement.scrollWidth,doc.documentElement.clientWidth),
            height: Math.max(doc.documentElement.scrollHeight,doc.documentElement.clientHeight)
        }
    }
}

function setIframeHeight(ifr){
	var doc = ifr.contentWindow.document;
	console.dir(doc)
	var size = getDocumentPort(doc)
	console.log(ifr)
	console.log("resize", size)
	ifr.height = size.height
}

function addIframe(e){
	var doc = e.target.contentWindow.document;
	var size = getDocumentPort(doc)
	console.log(size)
	e.target.height = document.documentElement.clientHeight - 160
	let fm = e.target
	// if(size.height==0){
	// 	setTimeout(()=>{
	// 		setIframeHeight(fm)
	// 	}, 5000)
	// }else{
	// 	e.target.height = document.documentElement.clientHeight - 100
	// }
}


window.onresize = function(){
	var fs = document.getElementsByClassName("itablet-iframe");
	// console.log(fs)
	for(var i=0; i< fs.length; i++){
		fs[i].height = document.documentElement.clientHeight - 160
	}
	
	// var c = document.getElementById("imainframe-content")
	// console.log(c)
	// console.log(c.clientHeight)
	
}